﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using Entity;

namespace DAL
{
    /// <summary>
    /// 紧固件信息访问类
    /// </summary>
    public class FastenerService
    {
        #region 查询紧固件数据库
        public List<Fastener> GetAll()
        {
            string sql = "select * from Fastener";
            SqlDataReader objReader = SQLHelper.GetReader(sql);
            List<Fastener> list = new List<Fastener>();
            while (objReader.Read())
            {
                list.Add(new Fastener()
                {
                    FastenerId= Convert.ToInt32(objReader["FastenerId"]),
                    FastenerName=objReader["FastenerName"].ToString(),
                    Para_D = Convert.ToSingle(objReader["Para_D"]),
                    Para_P = Convert.ToSingle(objReader["Para_P"]),
                    Para_H = Convert.ToSingle(objReader["Para_H"]),
                    Para_L = Convert.ToSingle(objReader["Para_L"])
                });
            }
            objReader.Close();
            return list;
        }
        #endregion

        #region 判断当前ID号是否已在数据库存在
        /// <summary>
        /// 判断当前ID号是否已在数据库存在
        /// </summary>
        /// <param name="FastenerId"></param>
        /// <returns></returns>
        public bool IsIdNoExisted(string FastenerId)
        {
            string sql = "select count(*) from Fastener where FastenerId ={0}";
            sql = string.Format(sql, FastenerId);
            int result = Convert.ToInt32(SQLHelper.GetSingleResult(sql));
            if (result == 1) return true;
            else return false;
        }


        #endregion

        public bool IsFastenerNameExisted(string FastenerId)
        {
            string sql = "select count(*) from Fastener where FastenerName ='{0}'";
            sql = string.Format(sql, FastenerId);
            int result = Convert.ToInt32(SQLHelper.GetSingleResult(sql));
            if (result == 1) return true;
            else return false;
        }



        #region 添加紧固件
        /// <summary>
        /// 添加紧固件对象信息到数据库
        /// </summary>
        /// <param name="objFastener"></param>
        /// <returns></returns>
        public int AddFastener(Fastener objFastener)
        {
            StringBuilder objBuilder = new StringBuilder();
            objBuilder.Append("insert into Fastener(FastenerName,FastenerId,Para_D,Para_P,Para_H,Para_L)");
            objBuilder.Append(" values('{0}',{1},{2},{3},{4},{5})");
            string sql = string.Format(objBuilder.ToString(),
                objFastener.FastenerName, objFastener.FastenerId, objFastener.Para_D,
                objFastener.Para_P, objFastener.Para_H, objFastener.Para_L);
            //提交到数据库
            try
            {
                return SQLHelper.Update(sql);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 更新紧固件,不可更改编号
        /// <summary>
        /// 更新紧固件信息到数据库
        /// </summary>
        /// <param name="objFastener"></param>
        /// <returns></returns>
        public int ModifyFastener(Fastener objFastener)
        {
            StringBuilder objBuilder = new StringBuilder();
            objBuilder.Append("Update Fastener set FastenerName='{0}',FastenerId={1},Para_D={2},Para_P={3},Para_H={4},Para_L={5}");
            objBuilder.Append(" where FastenerId={6}");
            string sql = string.Format(objBuilder.ToString(),
                objFastener.FastenerName, objFastener.FastenerId, objFastener.Para_D,
                objFastener.Para_P, objFastener.Para_H, objFastener.Para_L,objFastener.EidtId);
            //提交到数据库
            try
            {
                return SQLHelper.Update(sql);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

















        #region 添加New紧固件
        /// <summary>
        /// 添加紧固件对象信息到数据库
        /// </summary>
        /// <param name="objFastener"></param>
        /// <returns></returns>
        public int AddNewFastener(Fastener objFastener)
        {
            StringBuilder objBuilder = new StringBuilder();
            objBuilder.Append("insert into Fastener(FastenerName,FastenerId,Para_D,Para_P,Para_H,Para_L)");
            objBuilder.Append(" values('{0}',{1},{2},{3},{4},{5})");
            string sql = string.Format(objBuilder.ToString(),
                objFastener.FastenerName, objFastener.FastenerId, objFastener.Para_D,
                objFastener.Para_P, objFastener.Para_H, objFastener.Para_L);
            //提交到数据库
            try
            {
                return SQLHelper.Update(sql);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 更新New紧固件,不可更改编号
        /// <summary>
        /// 更新紧固件信息到数据库
        /// </summary>
        /// <param name="objFastener"></param>
        /// <returns></returns>
        public int ModifyNewFastener(Fastener objFastener)
        {
            StringBuilder objBuilder = new StringBuilder();
            objBuilder.Append("Update Fastener set Para_D={0},Para_P={1},Para_H={2},Para_L={3}");
            objBuilder.Append(" where FastenerName='{4}'");
            string sql = string.Format(objBuilder.ToString(),
                 objFastener.Para_D,
                objFastener.Para_P, objFastener.Para_H, objFastener.Para_L, objFastener.FastenerName);
            //提交到数据库
            try
            {
                return SQLHelper.Update(sql);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 删除紧固件
        /// <summary>
        /// 删除紧固件信息到数据库
        /// </summary>
        /// <param name="fastenerId"></param>
        /// <returns></returns>
        public int DeleteFastenerById(string fastenerId)
        {
            string sql = "delete from Fastener where FastenerId=" + fastenerId;            
            //提交到数据库
            try
            {
                return SQLHelper.Update(sql);
            }
            catch (SqlException ex)
            {
                if (ex.Number == 547)
                    throw new Exception("该紧固件被其他数据表引用，不能直接删除该紧固件对象！");
                else
                    throw new Exception("数据库操作出现异常！具体信息：" + ex.Message);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion


        #region 删除New紧固件
        /// <summary>
        /// 删除紧固件信息到数据库
        /// </summary>
        /// <param name="fastenerId"></param>
        /// <returns></returns>
        public int DeleteNewFastenerByName(string FastenerName)
        {
            string sql = "delete from Fastener where FastenerName=" + FastenerName;
            //提交到数据库
            try
            {
                return SQLHelper.Update(sql);
            }
            catch (SqlException ex)
            {
                if (ex.Number == 547)
                    throw new Exception("该紧固件被其他数据表引用，不能直接删除该紧固件对象！");
                else
                    throw new Exception("数据库操作出现异常！具体信息：" + ex.Message);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion


    }
}
